home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr49 / vesa18.zip / COMMON.C < prev    next >
C/C++ Source or Header  |  1995-01-28  |  11KB  |  440 lines

  1. /* VESA package for emx/gcc --- Copyright (c) 1993 by Johannes Martin */
  2. #include <sys/hw.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define INCL_VIO
  7. #define INCL_MOU
  8. #define INCL_DOSPROCESS
  9. #define INCL_DOSFILEMGR
  10.  
  11. #define VESADLL
  12.  
  13. #include <os2emx.h>
  14. #include <os2thunk.h>
  15.  
  16. #include "vgaports.h"
  17. #include "vesa.h"
  18. #include "vesadll.h"
  19. #include "common.h"
  20.  
  21. int        activeindex    = 0;
  22. VESASWITCH swfunc         = NULL;
  23. VESAWORD   ScanlineLength = 0;
  24. int        screenlocked   = 0;
  25. HMOU       MouseHandle    = 0;
  26.  
  27. void Error(char *s)
  28. {
  29.   ULONG dummy;
  30.  
  31.   DosWrite(2, s, strlen(s), &dummy);
  32. }
  33.  
  34. VESABOOL OS2VesaGetInfo(PVESAINFO pVesaInfo)
  35. {
  36.   pVesaInfo->Version = 0x102;
  37.   pVesaInfo->OEMName = "OS/2 VESA Driver by Johannes Martin";
  38.   pVesaInfo->Modes   = Modes;
  39.   return(TRUE);
  40. }
  41.  
  42. VESABOOL OS2VesaGetModeInfo(VESAWORD Mode, PVESAMODEINFO pVesaModeInfo)
  43. {
  44.   int i;
  45.  
  46.   Mode &= 0x7fff;
  47.   for (i = 0; Modes[i] != Mode; i++)
  48.     if (Modes[i] == 0xFFFF)
  49.       return(FALSE);
  50.   *pVesaModeInfo = ModeInfos[i].Vesa;
  51.   if ((i == activeindex) && OS2VesaLockScreen(0))
  52.     {
  53.       pVesaModeInfo->BytesPerScanline = ScanlineLength;
  54.       pVesaModeInfo->WindowAStart     = GetPhysBuf();
  55.       pVesaModeInfo->WindowBStart     = pVesaModeInfo->WindowAStart;
  56.       OS2VesaUnlockScreen();
  57.     }
  58.   if (pVesaModeInfo->NumberOfBitsPerPixel == 15)
  59.     pVesaModeInfo->NumberOfBitsPerPixel = 16;
  60.   return(TRUE);
  61. }
  62.  
  63. static void SetScanlineLength(void)
  64. {
  65.   if ((ScanlineLength <= 512) &&
  66.       ((ModeInfos[activeindex].Vesa.NumberOfBitsPerPixel == 4) ||
  67.        (ModeInfos[activeindex].Vesa.MemoryModelType == 5)))
  68.     {
  69.       _outp8(CRT_I, 0x17);
  70.       _outp8(CRT_D, _inp8(CRT_D) | 0x40);
  71.       _outp8(CRT_I, 0x13);
  72.       _outp8(CRT_D, ScanlineLength >> 1);
  73.     }
  74.   else
  75.     {
  76.       _outp8(CRT_I, 0x17);
  77.       _outp8(CRT_D, (_inp8(CRT_D) | 0x20) & ~0x40);
  78.       _outp8(CRT_I, 0x14);
  79.       _outp8(CRT_D, _inp8(CRT_D) | 0x40);
  80.       _outp8(CRT_I, 0x13);
  81.       _outp8(CRT_D, ScanlineLength >> 3);
  82.     }
  83. }
  84.  
  85. VESABOOL OS2VesaSetMode(VESAWORD Mode)
  86. {
  87.   USHORT status;
  88.   int i;
  89.  
  90.   if (OS2VesaLockScreen(0))
  91.     {
  92.       OS2VesaSetWindow(0, 0);
  93.       OS2VesaSetWindow(1, 0);
  94.       OS2VesaUnlockScreen();
  95.     }
  96.   for (i = 0; Modes[i] != Mode & 0x7fff; i++)
  97.     if (Modes[i] == 0xFFFF)
  98.       return(FALSE);
  99.   status = 0x100;
  100.   MouSetDevStatus(&status, MouseHandle);
  101.   if (!SetMode(i))
  102.     return(FALSE);
  103.   activeindex = i;
  104.   ScanlineLength = ModeInfos[i].Vesa.BytesPerScanline;
  105.   if (OS2VesaLockScreen(0))
  106.     {
  107.       SetSpecial(i);
  108.       if (ModeInfos[i].Vesa.ModeAttributes & 0x10)
  109.         {
  110.           if ((Mode & 0x8000) == 0)
  111.             {
  112.               PVOID Buffer   = GetPhysBuf();
  113.               int   LPWindow = ModeInfos[i].Vesa.WindowSize * 1024 / ModeInfos[i].Vesa.BytesPerScanline;
  114.               int   WinCount = ModeInfos[i].Vesa.Height / LPWindow
  115.                             + (ModeInfos[i].Vesa.Height % LPWindow != 0);
  116.               int   WinFact  = ModeInfos[i].Vesa.WindowSize / ModeInfos[i].Vesa.WindowGranularity;
  117.  
  118.               for (i = 0; i < WinCount; i++)
  119.                 {
  120.                   OS2VesaSetWindow(0, i * WinFact);
  121.                   memset(Buffer, 0, ModeInfos[activeindex].Vesa.WindowSize * 1024);
  122.                 }
  123.             }
  124.           OS2VesaSetWindow(0, 0);
  125.           OS2VesaSetWindow(1, 0);
  126.           SetScanlineLength();
  127.         }
  128.       OS2VesaUnlockScreen();
  129.     }
  130.   if (activeindex == 0)
  131.     {
  132.       status = 0;
  133.       MouSetDevStatus(&status, MouseHandle);
  134.     }
  135.   return(TRUE);
  136. }
  137.  
  138. VESABOOL OS2VesaGetMode(PVESAWORD Mode)
  139. {
  140.   *Mode = Modes[activeindex];
  141.   return(TRUE);
  142. }
  143.  
  144. VESABOOL OS2VesaSetScanlineLength(VESAWORD NumberOfPixels)
  145. {
  146.   ScanlineLength = NumberOfPixels * ModeInfos[activeindex].Vesa.NumberOfBitsPerPixel
  147.                     / (8 * ModeInfos[activeindex].Vesa.NumberOfMemoryPlanes);
  148.   if ((ScanlineLength <= 512) &&
  149.       ((ModeInfos[activeindex].Vesa.NumberOfBitsPerPixel == 4) ||
  150.        (ModeInfos[activeindex].Vesa.MemoryModelType == 5)))
  151.     ScanlineLength = (ScanlineLength + 1) & 0xFFFE;
  152.   else
  153.     ScanlineLength = (ScanlineLength + 7) & 0xFFF8;
  154.   if (OS2VesaLockScreen(0))
  155.     {
  156.       SetScanlineLength();
  157.       OS2VesaUnlockScreen();
  158.     }
  159.   return(TRUE);
  160. }
  161.  
  162. VESABOOL OS2VesaGetScanlineLength(PVESAWORD BytesPerScanline,
  163.                                   PVESAWORD NumberOfPixels,
  164.                                   PVESAWORD NumberOfScanlines)
  165. {
  166.   *BytesPerScanline  = ScanlineLength;
  167.   *NumberOfPixels    = ScanlineLength * ModeInfos[activeindex].Vesa.NumberOfMemoryPlanes
  168.                        * 8 / ModeInfos[activeindex].Vesa.NumberOfBitsPerPixel;
  169.   *NumberOfScanlines = ModeInfos[activeindex].Vesa.Height;
  170.   return(TRUE);
  171. }
  172.  
  173. static VOID APIENTRY RedrawWaitThread(ULONG Param)
  174. {
  175.   static VESAWORD WindowAStat = 0;
  176.   static VESAWORD WindowBStat = 0;
  177.  
  178.   USHORT notifytype;
  179.  
  180.   while (1)
  181.     {
  182.       VioSavRedrawWait(0, ¬ifytype, 0);
  183.       screenlocked++;
  184.       if (notifytype)
  185.         {
  186.           SetMode(activeindex);
  187.           SetSpecial(activeindex);
  188.           SetScanlineLength();
  189.           OS2VesaSetWindow(0, WindowAStat);
  190.           OS2VesaSetWindow(1, WindowBStat);
  191.         }
  192.       swfunc(notifytype);
  193.       if (!notifytype)
  194.         {
  195.           OS2VesaGetWindow(0, &WindowAStat);
  196.           OS2VesaGetWindow(1, &WindowBStat);
  197.           SetMode(0);
  198.           SetSpecial(0);
  199.         }
  200.       screenlocked--;
  201.     }
  202. }
  203.  
  204. VESABOOL OS2VesaSetSwitchFunc(VESASWITCH SwitchFunc)
  205. {
  206.   static TID tid = 0;
  207.  
  208.   if ((VioSavRedrawUndo(1, 1, 0) != 0) && (tid != 0))
  209.     DosKillThread(tid);
  210.   if ((swfunc = SwitchFunc) != NULL)
  211.     return(DosCreateThread(&tid, RedrawWaitThread, 0, 0, 32 * 1024) == 0);
  212.   else
  213.     {
  214.       tid = 0;
  215.       return(TRUE);
  216.     }
  217. }
  218.  
  219. VESABOOL OS2VesaLockScreen(VESACHAR wait)
  220. {
  221.   UCHAR Success;
  222.  
  223.   if ((screenlocked) ||
  224.       ((VioScrLock(wait, &Success, 0) == 0) && (wait || (Success == 0))))
  225.     {
  226.       screenlocked++;
  227.       return(TRUE);
  228.     }
  229.   else
  230.     return(FALSE);
  231. }
  232.  
  233. VESABOOL OS2VesaUnlockScreen(void)
  234. {
  235.   if (screenlocked)
  236.     if (--screenlocked)
  237.       return(TRUE);
  238.     else
  239.       return(VioScrUnLock(0) == 0);
  240.   else
  241.     return(FALSE);
  242. }
  243.  
  244. VESABOOL OS2VesaSetModified(void)
  245. {
  246.   return(TRUE);
  247. }
  248.  
  249. VESABOOL OS2VesaSetPalette(VESAWORD   StartIndex,
  250.                            VESAWORD   NumberOfEntries,
  251.                            PCVESACHAR Palette,
  252.                            VESABOOL   Wait)
  253. {
  254.   if ((NumberOfEntries >= 1) && (StartIndex + NumberOfEntries <= 256))
  255.     {
  256.       if (!OS2VesaLockScreen(0))
  257.         return(FALSE);
  258.       if (Wait)
  259.         _wait01 (IS1_R, 0x08);
  260.       _outp8 (PEL_IW, StartIndex);
  261.       _outps8dac (PEL_D, Palette, 3 * NumberOfEntries);
  262.       OS2VesaUnlockScreen();
  263.       return(TRUE);
  264.     }
  265.   return(FALSE);
  266. }
  267.  
  268. VESABOOL OS2VesaGetPalette(VESAWORD  StartIndex,
  269.                            VESAWORD  NumberOfEntries,
  270.                            PVESACHAR Palette)
  271. {
  272.   if ((NumberOfEntries >= 1) && (StartIndex + NumberOfEntries <= 256))
  273.     {
  274.       int i, j;
  275.  
  276.       if (!OS2VesaLockScreen(0))
  277.         return(FALSE);
  278.       /* select palette register */
  279.       _outp8(PEL_IR, StartIndex);
  280.  
  281.       for(j = 0; j < NumberOfEntries; j++)
  282.         {
  283.           for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  284.           *(Palette++) = (int) _inp8(PEL_D);
  285.           for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  286.           *(Palette++) = (int) _inp8(PEL_D);
  287.           for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  288.           *(Palette++) = (int) _inp8(PEL_D);
  289.         }
  290.       OS2VesaUnlockScreen();
  291.       return(TRUE);
  292.     }
  293.   return(FALSE);
  294. }
  295.  
  296. static ULONG LastCode = -1;
  297.  
  298. VESABOOL OS2VesaGetCharacter(PVESACHAR pchar)
  299. {
  300.   if (LastCode != -1)
  301.     {
  302.       *pchar   = LastCode;
  303.       LastCode = -1;
  304.     }
  305.   else
  306.     *pchar = _read_kbd(0, 1, 0);
  307.   return(TRUE);
  308. }
  309.  
  310. VESABOOL OS2VesaKeyboardHit(void)
  311. {
  312.   if (LastCode == -1)
  313.     LastCode = _read_kbd(0, 0, 0);
  314.   return(LastCode != -1);
  315. }
  316.  
  317. VESABOOL OS2MouseOpen(void)
  318. {
  319.   if (MouseHandle)
  320.     {
  321.       USHORT mask = 0x7F;
  322.  
  323.       MouFlushQue(MouseHandle);
  324.       MouSetEventMask(&mask, MouseHandle);
  325.       return(TRUE);
  326.     }
  327.   return(FALSE);
  328. }
  329.  
  330. VESABOOL OS2MouseClose(void)
  331. {
  332.   return(MouseHandle != 0);
  333. }
  334.  
  335. VESABOOL OS2MouseShow(void)
  336. {
  337.   if (MouseHandle && !activeindex)
  338.     {
  339.       MouDrawPtr(MouseHandle);
  340.       return(TRUE);
  341.     }
  342.   return(FALSE);
  343. }
  344.  
  345. VESABOOL OS2MouseHide(void)
  346. {
  347.   if (MouseHandle && !activeindex)
  348.     {
  349.       NOPTRRECT area;
  350.  
  351.       area.col  = 0;
  352.       area.row  = 0;
  353.       area.cCol = 65535;
  354.       area.cRow = 65535;
  355.       MouRemovePtr(&area, MouseHandle);
  356.       return(TRUE);
  357.     }
  358.   return(FALSE);
  359. }
  360.  
  361. VESABOOL OS2MouseSetPosition(VESAWORD x, VESAWORD y)
  362. {
  363.   if (MouseHandle)
  364.     {
  365.       PTRLOC location;
  366.  
  367.       location.row = y;
  368.       location.col = x;
  369.       MouSetPtrPos(&location, MouseHandle);
  370.       return(TRUE);
  371.     }
  372.   return(FALSE);
  373. }
  374.  
  375. VESABOOL OS2MouseGetPosition(PVESAWORD x, PVESAWORD y)
  376. {
  377.   if (MouseHandle)
  378.     {
  379.       PTRLOC location;
  380.  
  381.       MouGetPtrPos(&location, MouseHandle);
  382.       *x = location.col;
  383.       *y = location.row;
  384.       return(TRUE);
  385.     }
  386.   return(FALSE);
  387. }
  388.  
  389. VESABOOL OS2MouseGetEvent(PVESAWORD x, PVESAWORD y, PVESAWORD status, VESABOOL wait)
  390. {
  391.   static VESAWORD sstatus = 0;
  392.  
  393.   if (MouseHandle)
  394.     {
  395.       MOUEVENTINFO event;
  396.       USHORT uswait, rc;
  397.  
  398.       uswait = wait;
  399.       rc     = MouReadEventQue(&event, &uswait, MouseHandle);
  400.       if (rc == 0)
  401.         {
  402.           *x = event.col;
  403.           *y = event.row;
  404.           if (event.fs & 0x06)
  405.             sstatus = 1;
  406.           else
  407.             sstatus = 0;
  408.           if (event.fs & 0x18)
  409.             sstatus |= 2;
  410.           if (event.fs & 0x60)
  411.             sstatus |= 4;
  412.           *status = sstatus;
  413.           return(TRUE);
  414.         }
  415.     }
  416.   OS2MouseGetPosition(x, y);
  417.   *status = sstatus;
  418.   return(FALSE);
  419. }
  420.  
  421. int _CRT_init(void);
  422. void _CRT_term(void);
  423.  
  424. unsigned long _DLL_InitTerm(unsigned long modhandle, unsigned long flag)
  425. {
  426.   if (flag)
  427.     {
  428.       if (activeindex)
  429.         OS2VesaSetMode(0x03);
  430.       if (MouseHandle)
  431.         MouClose(MouseHandle);
  432.       ClearUp();
  433.       _CRT_term();
  434.       return(TRUE);
  435.     }
  436.   if (MouOpen(NULL, &MouseHandle) && MouOpen("POINTER$", &MouseHandle))
  437.     MouseHandle = 0;
  438.   return((_CRT_init() == 0) && Initialize());
  439. }
  440.